

#import "PUTipView.h"
@interface PUTipView()
@property(nonatomic,retain)UIView *nohahaAlphaView;
@property(nonatomic,retain)NSString *content;
@property(nonatomic,retain)NSString *confirmStr;
@property(nonatomic,retain)NSString *cancelStr;
@property(nonatomic,retain)NSString *titleStr;
@property(nonatomic)int buttonCount;
@property(nonatomic)int type;
@property(nonatomic,retain)NSString *rightEvent;
@property(nonatomic,retain)NSString *leftEvent;

@end
@implementation PUTipView
-(instancetype)initCPTipViewWithContent:(NSString *)content  title:(NSString *)title confirmStr:(NSString *)confirmStr cancelStr:(NSString *)cancelStr buttonCount:(int)buttonCount
{
    self = [super init];
    if (self) {
   
        self.frame = [UIScreen mainScreen].bounds;
//        self.backgroundColor = [UIColor blackColor];
//        self.alpha = 0.3;
        [[UIApplication sharedApplication].delegate.window addSubview:self];
        self.content=content;
        self.confirmStr=confirmStr;
        self.cancelStr=cancelStr;
        self.titleStr=title;
        self.buttonCount=buttonCount;
        [self _initViews];
    }
    return self;
}

-(instancetype)initCPTipViewWithContent:(NSString*)content title:(NSString *)title confirmStr:(NSString*)confirmStr cancelStr:(NSString*)cancelStr buttonCount:(int)buttonCount type:(int)type rightEvent:(nonnull NSString *)rightEvent leftEvent:(nonnull NSString *)leftEvent{
    self = [super init];
    if (self) {
   
        self.frame = [UIScreen mainScreen].bounds;
//        self.backgroundColor = [UIColor blackColor];
//        self.alpha = 0.3;
        [[UIApplication sharedApplication].delegate.window addSubview:self];
        self.content=content;
        self.confirmStr=confirmStr;
        self.cancelStr=cancelStr;
        self.titleStr=title;
        self.buttonCount=buttonCount;
        self.type=type;
        self.rightEvent=rightEvent;
        self.leftEvent=leftEvent;
        [self _initViews];
    }
    return self;
}

-(void)_initViews{
    
    self.nohahaAlphaView = [[UIView alloc]init];
    self.nohahaAlphaView.frame =self.bounds;
    self.nohahaAlphaView.alpha = 0.3;
    self.nohahaAlphaView.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0];
    [self addSubview:self.nohahaAlphaView];
    
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor=[UIColor whiteColor];
    bgView.center = self.center;
    bgView.bounds = CGRectMake(0, 0, self.frame.size.width*0.786, self.frame.size.width*0.786*0.596);
    bgView.layer.masksToBounds = YES;
    bgView.layer.cornerRadius = self.frame.size.width*0.786*0.067;
    [self addSubview:bgView];
    
    
    UIImageView *bgImage=[[UIImageView alloc]init];
    bgImage.frame=bgView.bounds;
//    bgImage.image=[UIImage imageNamed:@"bgView"];
    [bgView addSubview:bgImage];
    
    
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(0,bgView.frame.size.height*0.111,bgView.frame.size.width,bgView.frame.size.height*0.111);
//    label.backgroundColor=[UIColor blueColor];
    label.textAlignment=NSTextAlignmentCenter;
    label.text=self.titleStr;
    label.font=[UIFont boldSystemFontOfSize:19];
    label.textColor = [UIColor colorWithHex:0x3C3C3C];
    [bgView addSubview:label];
    
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.frame = CGRectMake(20,bgView.frame.size.height*0.338,bgView.frame.size.width-40,bgView.frame.size.height*0.223);
//    label.backgroundColor=[UIColor blueColor];
    label2.textAlignment=NSTextAlignmentCenter;
    label2.text=self.content;
    label2.font=[UIFont systemFontOfSize:14];
    label2.textColor = [UIColor blackColor];
    label2.numberOfLines=0;
    [bgView addSubview:label2];
    
    
    if (self.buttonCount==2) {
        //左边按钮
        UIButton *updateButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [updateButton setBackgroundColor:[UIColor colorWithHex:0xEBEBEB]];
        updateButton.frame = CGRectMake(bgView.frame.size.width*0.054, bgView.frame.size.height*0.636, bgView.frame.size.width*0.42, bgView.frame.size.width*0.42*0.354);
        updateButton.clipsToBounds = YES;
        updateButton.layer.cornerRadius =updateButton.frame.size.height/2;
    //    updateButton.layer.borderWidth = 1.0f;
    //    updateButton.layer.borderColor = [[UIColor colorWithHex:0xBCBCBC] CGColor];
        [updateButton addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [updateButton setTitle:self.cancelStr forState:UIControlStateNormal];
         updateButton.titleLabel.font = [UIFont systemFontOfSize: 16.0];
        [updateButton setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
        updateButton.tag=1000;
        [bgView addSubview:updateButton];
        
        //右边按钮
        UIButton *rechargeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [rechargeBtn setBackgroundColor:[UIColor colorWithHex:0xFF729A]];
        rechargeBtn.frame = CGRectMake(bgView.frame.size.width*0.525,  bgView.frame.size.height*0.636,bgView.frame.size.width*0.42, bgView.frame.size.width*0.42*0.354);
        rechargeBtn.clipsToBounds = YES;
        rechargeBtn.layer.cornerRadius =rechargeBtn.frame.size.height/2;
        [rechargeBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [rechargeBtn setTitle:self.confirmStr forState:UIControlStateNormal];
         rechargeBtn.titleLabel.font = [UIFont systemFontOfSize: 16.0];
        [rechargeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        rechargeBtn.tag=1001;
        [bgView addSubview:rechargeBtn];
    }else{
        //一个按钮
        UIButton *updateButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [updateButton setBackgroundColor:[UIColor colorWithHex:0xFF729A]];
        updateButton.frame = CGRectMake(bgView.frame.size.width*0.149, bgView.frame.size.height*0.636, bgView.frame.size.width*0.701, bgView.frame.size.width*0.701*0.212);
        updateButton.clipsToBounds = YES;
        updateButton.layer.cornerRadius =updateButton.frame.size.height/2;
    //    updateButton.layer.borderWidth = 1.0f;
    //    updateButton.layer.borderColor = [[UIColor colorWithHex:0xBCBCBC] CGColor];
        [updateButton addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [updateButton setTitle:self.confirmStr forState:UIControlStateNormal];
         updateButton.titleLabel.font = [UIFont systemFontOfSize: 16.0];
        [updateButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        updateButton.tag=1001;
        [bgView addSubview:updateButton];
    }
    
}

-(void)btnClicked:(UIButton*)button{
    [self disMiss];
    //前往查看
    int index=0;
    //继续存证
    if (button.tag==1001) {
        index=1;
    }
    if (self.type==1) {//系统消息
        if ([self.delegate respondsToSelector:@selector(btnClickedTypeIndex:rightEvent:leftEvent:)]) {
            [self.delegate btnClickedTypeIndex:index rightEvent:self.rightEvent leftEvent:self.leftEvent];
        }
    }else{
        if ([self.delegate respondsToSelector:@selector(btnClickedIndex:)]) {
            [self.delegate btnClickedIndex:index];
        }
    }
  
}
-(void)show
{
    CGRect frame = self.frame;
    frame.origin.y = 0;
    [UIView animateWithDuration:0.4 animations:^{
        self.frame = frame;
    }];
}
-(void)disMiss{

    [UIView animateWithDuration:0.05 animations:^{
        self.backgroundColor = [UIColor clearColor];
        self.alpha = 0;
        
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}



@end
